package com.zisuyi.uilibrary.base

import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewbinding.ViewBinding
import com.weilele.mvvm.base.helper.appCompatActivity
import com.weilele.mvvm.base.helper.createViewBinding
import com.weilele.mvvm.base.helper.ignoreError
import com.weilele.mvvm.utils.activity.*
import com.weilele.mvvm.utils.android_r.fullScreen
import com.weilele.mvvm.widget.BaseLinearLayout
import com.zisuyi.game.sdk.base.BaseActivity
import com.zisuyi.game.sdk.base.SwitchView
import com.zisuyi.uilibrary.R
import com.zisuyi.uilibrary.view.AmountTextView
import com.zisuyi.uilibrary.view.ClickScaleImageView
import com.zisuyi.uilibrary.view.FontTextView

abstract class BaseUiActivity<Binding : ViewBinding?> : BaseActivity<Binding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        ignoreError {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        super.onCreate(savedInstanceState)
        fullScreen(true)
    }

    private var _binding: Binding? = null
    override val mBinding: Binding
        get() = _binding!!


    override fun getLayoutResOrView(inflater: LayoutInflater, parent: ViewGroup?): Any? {
        val title = getTitleBarTitle()
        return if (title != null) {
            val linearLayout = LinearLayout(appCompatActivity)
            linearLayout.orientation = LinearLayout.VERTICAL
            linearLayout.addView(
                BaseLinearLayout(this).apply {
                    setPadding(0, dip(24), 0, 0)
                    setBackgroundResource(R.color.colorToolbar)
                    gravity = Gravity.CENTER_VERTICAL
                    //返回按钮
                    addView(
                        ClickScaleImageView(appCompatActivity).apply {
                            scaleType = ImageView.ScaleType.CENTER
                            setImageResource(R.drawable.icon_back)
                            onClick {
                                onBackPressed()
                            }
                        },
                        LinearLayout.LayoutParams(
                            dip(40), dip(40)
                        ).apply {
                            marginStart = dip(8)
                            marginEnd = dip(8)
                        }
                    )
                    //标题
                    addView(
                        FontTextView(appCompatActivity).apply {
                            text = title
                            textSize = 20f
                            setTextColor(getResColor(R.color.colorText))
                        },
                        LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT
                        )
                    )
                    //间隔
                    addView(
                        FontTextView(appCompatActivity),
                        LinearLayout.LayoutParams(
                            dip(0), dip(0)
                        ).apply {
                            weight = 1f
                        }
                    )
                    if (isNeedCoinView()) {
                        addView(
                            AmountTextView(appCompatActivity),
                            LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT
                            ).apply {
                                marginEnd = dip(16)
                            }
                        )
                    }
                },
                LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, dip(60)
                )
            )
            //分割线
            linearLayout.addView(
                View(appCompatActivity).apply {
                    setBackgroundColor(Color.parseColor("#5A604E"))
                }, LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    dip(3)
                )
            )
            linearLayout.addView(switchUiView)
            linearLayout
        } else {
            switchUiView
        }
    }

    private val switchUiView by lazy {
        SwitchView(this) {
            val binding = createViewBinding<Binding>(layoutInflater, null, it, false)
            _binding = binding
            binding!!.root
        }
    }

    override fun onResume() {
        super.onResume()
        fullScreen(true)
    }

    override fun showOtherView(
        message: String?,
        drawable: Int?,
        retryText: String?,
        onSingleClick: ((View) -> Unit)?
    ) {
        switchUiView.showOtherView(message, drawable, retryText, onSingleClick)
    }

    override fun showLoadingView(
        message: String?,
        isHideContentView: Boolean,
        isEnableContentView: Boolean
    ) {
        switchUiView.showLoadingView(message, isHideContentView, isEnableContentView)
    }

    override fun showContentView() {
        switchUiView.showContentView()
    }

    final override fun isNeedToolbar(): Boolean {
        return false
    }

    final override fun isNeedBackIcon(): Boolean {
        return super.isNeedBackIcon()
    }

    open fun getTitleBarTitle(): CharSequence? {
        return null
    }

    open fun isNeedCoinView(): Boolean {
        return false
    }
}